<?php

//update these variables based on the dimensions of your local IE window:
$crop = true; //set this to false if you don't want image cropped
$crop_top = 97; //in pixels
$crop_right = 26; //pay attention to scrollbars
$crop_bottom = 49; //pay attention to scrollbars
$crop_left = 9;

//grab variables from the query string
$url = trim(strip_tags(urldecode($_GET['url'])));
if (!$url) { die('Error! No URL.'); }
$height = intval(@$_GET['height']);
if (!$height) { $height = 800; }
$width = intval(@$_GET['width']);
if (!$width) { $width = 1000; }
$resized_width = intval($_GET['resized_width']);

//open up a new Internet Explorer window and go to the specified URL
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Width = $width;
$browser->Height = $height;
$browser->Navigate($url);

//wait for it to finish loading
while ($browser->Busy) {
    com_message_pump(4000);
}
sleep(5); //give it an extra 5 seconds to load images, videos, etc.

//take snapshot
$im = imagegrabwindow($handle);

//close down Internet Explorer
$browser->Quit();

//check to be sure the image exists
if (!$im) { die('Error! Image could not be created!'); }

//crop the image to get rid of menu, scrollbars, etc.
if ($crop) {
    $new_width = $width - $crop_left - $crop_right;
    if ($new_width < 1) { die('Error! Crop width cannot be less than 1 pixel!'); }
    $new_height = $height - $crop_top - $crop_bottom;
    if ($new_height < 1) { die('Error! Crop height cannot be less than 1 pixel!'); }
    $im2 = imagecreatetruecolor($new_width, $new_height);
    imagecopy($im2, $im, 0, 0, $crop_left, $crop_top, $new_width, $new_height) or die('Error! Could not crop image.');
    imagedestroy($im); //destroy the original
    $image = 'im2';
} else { $image = 'im'; }

//resize the image
if ($resized_width) {
    $new_width = $resized_width;
    $new_height = round(( $new_width / imagesx($$image) ) * imagesy($$image));
    $im3 = imagecreatetruecolor($new_width,$new_height);
    imagecopyresized ($im3, $$image, 0, 0, 0, 0, $new_width, $new_height, imagesx($$image), imagesy($$image)) or die('Error! Could not resize image.');
    imagedestroy($$image); //destroy the original
    $image = 'im3';
}

//display the image, then destroy it
header('Content-Type: image/jpeg');
imagejpeg($$image);
imagedestroy($$image);

echo "screenshot.php";

?>

